package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.dto.responsebody.CollectItemResponseBody;
import com.yuesystem.entity.Academy;
import com.yuesystem.entity.Blog;
import com.yuesystem.entity.CollectBlog;
import com.yuesystem.entity.CollectItem;
import com.yuesystem.entity.Office;
import com.yuesystem.mapper.CollectItemMapper;
import com.yuesystem.service.AcademyService;
import com.yuesystem.service.BlogService;
import com.yuesystem.service.CollectItemService;
import com.yuesystem.service.OfficeService;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 收藏条目接口实现类.
 */
@Service
@Slf4j
public class CollectItemServiceImpl extends
    ServiceImpl<CollectItemMapper, CollectItem> implements CollectItemService {
  public static Long getUserId() {
    return userId;
  }

  public static void setUserId(Long userId) {
    CollectItemServiceImpl.userId = userId;
  }

  private static Long userId;
  @Autowired
  OfficeService officeService;
  @Autowired
  AcademyService academyService;
  @Autowired
  BlogService blogService;

  /**
   * 向Item表中增加一条记录.
   *
   * @param folderId 所属收藏夹id
   * @param type     类型（官职/书院/帖子）
   * @param id       （官职/书院/帖子id）
   * @return 操作是否成功
   */
  @Override
  public Boolean add(Long folderId, String type, Long id) {
    Long currentId = userId;
    CollectItem collectItem = new CollectItem();
    collectItem.setFolderId(folderId);
    collectItem.setType(type);
    collectItem.setCollectId(id);
    collectItem.setUserId(currentId);
    boolean save = this.save(collectItem);
    return save;
  }

  /**
   * 在Item表中删除一条记录.
   *
   * @param itemId 收藏条目id（主键）
   * @return 操作是否成功
   */
  @Override
  public Boolean cancel(Long itemId) {
    boolean remove = removeById(itemId);
    return remove;
  }

  /**
   * 判断当前（官职/书院/帖子）条目是否被收藏过.
   *
   * @param collectId （官职/书院/帖子id）
   * @param type      类型（官职/书院/帖子）
   * @return 被收藏的条目id
   */
  @Override
  public Long judge(Long collectId, String type) {
    Long currentId = userId;
    LambdaQueryWrapper<CollectItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(CollectItem::getUserId, currentId)
        .eq(CollectItem::getType, type)
        .eq(CollectItem::getCollectId, collectId);
    CollectItem one = this.getOne(queryWrapper);
    if (one == null) {
      return -1L;
    }
    return one.getId();
  }

  /**
   * 获取某个用户的某个收藏夹下的所有条目.
   *
   * @param folderId 收藏夹id
   * @return 收藏条目响应类列表
   */
  @Override
  public List<CollectItemResponseBody> getAllItem(Long folderId) {
    Long currentId = userId;
    LambdaQueryWrapper<CollectItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(CollectItem::getUserId, currentId)
        .eq(CollectItem::getFolderId, folderId);
    List<CollectItem> itemList = list(queryWrapper);
    List<CollectItemResponseBody> list = new ArrayList<>();
    for (CollectItem collectItem : itemList) {
      CollectItemResponseBody body = new CollectItemResponseBody();
      body.setCollectId(collectItem.getCollectId());
      body.setType(collectItem.getType());
      body.setId(collectItem.getId());
      if (collectItem.getType().equals("office")) {
        Office office = officeService.getById(collectItem.getCollectId());
        body.setItemName(office.getOfficeName());
      } else if (collectItem.getType().equals("academy")) {
        Academy academy = academyService.getById(collectItem.getCollectId());
        body.setItemName(academy.getAcademyName());
      } else if (collectItem.getType().equals("blog")) {
        Blog blog = blogService.getById(collectItem.getCollectId());
        body.setItemName(blog.getTitle());
      }
      list.add(body);
    }
    return list;
  }

  /**
   * 根据blog的id删除所有和他有关的收藏.
   *
   * @param listBlog 帖子id列表
   */
  @Override
  public void deleteByBlog(List<Long> listBlog) {
    LambdaQueryWrapper<CollectItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(CollectItem::getType,"blog");
    queryWrapper.in(listBlog.size() != 0, CollectItem::getCollectId, listBlog);
    this.remove(queryWrapper);
  }

}
